package cn.com.eado.crm.app.utils;

import android.content.Context;
import android.content.SharedPreferences;

/** SharedPreferences util
 * Created by Administrator on 2017/12/2.
 */

public class SpUtil {
    private final static String spFileName = "sp_file";
    private static SharedPreferences sp = null;

    public static void writeBoolean(Context context, String key, boolean value){
        if (sp == null){
            sp = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).apply();
    }

    public static boolean readBoolean(Context context, String key, boolean defaultValue){
        if (sp == null){
            sp = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defaultValue);
    }

    public synchronized static void writeInt(Context context, String key, int value){
        if (sp == null){
            sp = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        }
        sp.edit().putInt(key, value).apply();
    }

    public static int readInt(Context context, String key, int defaultValue){
        if (sp == null){
            sp = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        }
        return sp.getInt(key, defaultValue);
    }

    public static void writeString(Context context, String key, String value){
        if (sp == null){
            sp = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        }
        sp.edit().putString(key, value).apply();
    }

    public static String readString(Context context, String key, String defaultValue){
        if (sp == null){
            sp = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        }
        return sp.getString(key, defaultValue);
    }
}
